//
//  XJOriginalView.swift
//  新浪微博
//
//  Created by 徐绩 on 16/4/20.
//  Copyright © 2016年 ayumi. All rights reserved.
//

import UIKit
import SnapKit
import YYText

//昵称大小
let nameLabelFont: CGFloat = 15
//时间大小
let timeLabelFont: CGFloat = 12
//来源
let sourceLabelFont: CGFloat = 12
//margin
let cellMargin: CGFloat = 10
//头像大小
let iconSizeWidth:CGFloat = 35
//内容字体
let contentLabelFont: CGFloat = 15


class XJOriginalView: UIView {
    
    var bottomCons: Constraint?
    //为控件赋值数据
    var statusModel:XJHomeCellViewModel?{
    
        didSet{
            
            // 设置头像
            iconImageView.sd_setImageWithURL(NSURL(string: statusModel?.status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_small"))
            // 设置昵称
            nameLabel.text = statusModel?.status?.user?.name
            //会员
            memberImage.image = statusModel?.memberImage
            //时间
            timeLabel.text = statusModel?.createAtText
            //来源
            sourceLabel.text = statusModel?.sourceText
            //正文
//            contentLabel.text = statusModel?.status?.text
            contentLabel.attributedText = statusModel?.originalAttributedStr
            
            //设置配图数据,根据有无视图,更新约束
            bottomCons?.uninstall()
            if let pic_urls = statusModel?.status?.pic_urls where pic_urls.count > 0{
 
               picImageView.dataInfo = (pic_urls, statusModel!.originalPictureViewSize)
                picImageView.hidden = false
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                   bottomCons = make.bottom.equalTo(picImageView).offset(cellMargin).constraint
                })
            }else{
                
                picImageView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    bottomCons = make.bottom.equalTo(contentLabel).offset(cellMargin).constraint
                })
            
            }
           
        }
    }

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     布局原创微博界面
     */
    private func setupUI() {
    
        backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(iconImageView)
        addSubview(nameLabel)
        addSubview(memberImage)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(picImageView)
    
        /**
        *  添加约束
        */
        //头像
        iconImageView.snp_makeConstraints { (make) -> Void in
             make.top.leading.equalTo(cellMargin)
            make.width.height.equalTo(iconSizeWidth)
            
        }
        //昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView)
            make.leading.equalTo(iconImageView.snp_trailing).offset(cellMargin)
        }
        //会员
        memberImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView)
            make.leading.equalTo(nameLabel.snp_trailing).offset(cellMargin)
        }
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(iconImageView.snp_bottom)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(cellMargin)
        }
        //认证
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView.snp_trailing).offset(-4)
            make.centerY.equalTo(iconImageView.snp_bottom).offset(-4)
        }
        //内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp_bottom).offset(cellMargin)
        }
        //配图视图
        picImageView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
//            make.size.equalTo(CGSizeMake(200, 200))
        }
        
        self.snp_makeConstraints { (make) -> Void in
         bottomCons = make.bottom.equalTo(picImageView.snp_bottom).offset(cellMargin).constraint
        }
        
        
        
    }
    
     /// 懒加载控件
    //头像
   private lazy var iconImageView: UIImageView = {
        
        let iconImage = UIImageView(image: UIImage(named: "avatar_default_small"))
        iconImage.layer.cornerRadius = 18
        iconImage.layer.masksToBounds = true
        return iconImage
    }()
    //昵称
    private lazy var nameLabel: UILabel = {
        
        let nameLabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: nameLabelFont)
        return nameLabel
    }()
    
    //会员
    private lazy var memberImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //时间
    private lazy var timeLabel: UILabel = {
        
        let timeLabel = UILabel(textColor: UIColor.orangeColor(), fontSize: timeLabelFont)
        return timeLabel
    }()
    
    //来源
    private lazy var sourceLabel: UILabel = {
        
        let sourceLabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: sourceLabelFont)
        return sourceLabel
    }()
    
    //认证
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //内容
    private lazy var contentLabel: YYLabel = {
    
//        let contentLabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: contentLabelFont, preferredMaxLayoutWidth: SCREENW - 2 * cellMargin)
        let contentLabel = YYLabel()
        contentLabel.textColor = UIColor.darkGrayColor()
        contentLabel.font = UIFont.systemFontOfSize(contentLabelFont)
        contentLabel.preferredMaxLayoutWidth = SCREENW - 2 * cellMargin
        contentLabel.numberOfLines = 0
        contentLabel.userInteractionEnabled = true
        
        return contentLabel
    }()
    
    //配图视图
    private lazy var picImageView: XJStatusPictureView = XJStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    

    
    
}
